<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import OutpatientApi from '@/utils/Interceptors'
import { showToast } from 'vant'

const router = useRouter()
const categories = ref<any[]>([])
const consultations = ref<any[]>([])
const loading = ref(false)
const consultationsLoading = ref(false)
const selectedCategoryId = ref<number | null>(null)
const selectedCategoryName = ref<string>('')
const isDevelopmentMode = ref(false)

// 获取咨询分类
const getConsultationCategories = async () => {
  try {
    loading.value = true
    const response = await OutpatientApi.get('/api/ConsultationCategory/GetAllCategories')
    
    if (response.data.code === 200) {
      categories.value = response.data.data
      // 默认选择第一个分类
      if (categories.value.length > 0) {
        selectCategory(categories.value[0])
      }
    } else {
      showToast({
        message: response.data.msg || '获取分类失败',
        position: 'middle',
        duration: 2000
      })
    }
  } catch (error) {
    console.error('获取咨询分类失败:', error)
    // 如果API不可用，显示开发模式
    isDevelopmentMode.value = true
    showToast({
      message: '功能开发中，请稍后重试',
      position: 'middle',
      duration: 2000
    })
  } finally {
    loading.value = false
  }
}

// 获取咨询信息列表
const getConsultationsByCategoryId = async (categoryId: number) => {
  try {
    consultationsLoading.value = true
    const response = await OutpatientApi.get('/api/ConsultationCategory/GetConsultationsByCategoryId', {
      params: {
        categoryId: categoryId,
        pageIndex: 1,
        pageSize: 10
      }
    })
    
    if (response.data.code === 200) {
      consultations.value = response.data.data
    } else {
      showToast({
        message: response.data.msg || '获取咨询信息失败',
        position: 'middle',
        duration: 2000
      })
    }
  } catch (error) {
    console.error('获取咨询信息失败:', error)
    showToast({
      message: '网络错误，请稍后重试',
      position: 'middle',
      duration: 2000
    })
  } finally {
    consultationsLoading.value = false
  }
}

// 返回首页
const goBack = () => {
  router.push('/')
}

// 选择分类
const selectCategory = (category: any) => {
  selectedCategoryId.value = category.CategoryId
  selectedCategoryName.value = category.CategoryName
  getConsultationsByCategoryId(category.CategoryId)
}

// 跳转到咨询详情
const goToDetail = (consultation: any) => {
  console.log('跳转到详情页面:', consultation)
  router.push(`/consultation-detail/${consultation.ConsultId}`)
}

onMounted(() => {
  getConsultationCategories()
})
</script>

<template>
  <div class="consultation-container">
    <!-- 状态栏 -->
    <div class="status-bar">
      <span class="time">12:30</span>
      <div class="status-icons">
        <span class="signal">📶</span>
        <span class="wifi">📶</span>
        <span class="battery">🔋</span>
      </div>
    </div>

    <!-- 头部 -->
    <div class="header">
      <div class="back-button" @click="goBack">
        <span class="back-icon">←</span>
      </div>
      <h1 class="page-title">健康资讯栏目</h1>
      <div class="header-actions">
        <span class="more-icon">⋯</span>
        <span class="view-icon">👁</span>
      </div>
    </div>

    <!-- 开发模式提示 -->
    <div v-if="isDevelopmentMode" class="development-notice">
      <div class="notice-content">
        <div class="notice-icon">🚧</div>
        <h3 class="notice-title">功能开发中</h3>
        <p class="notice-text">健康咨询功能正在开发中，敬请期待！</p>
        <div class="notice-features">
          <div class="feature-item">
            <span class="feature-icon">📋</span>
            <span>咨询分类浏览</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">👨‍⚕️</span>
            <span>医生专业回复</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">💬</span>
            <span>互动交流功能</span>
          </div>
        </div>
        <button class="back-home-btn" @click="goBack">返回首页</button>
      </div>
    </div>

    <!-- 正常功能界面 -->
    <div v-else>
      <!-- 分类标签 -->
      <div class="category-tabs">
        <div 
          v-for="category in categories" 
          :key="category.CategoryId"
          class="category-tab"
          :class="{ active: selectedCategoryId === category.CategoryId }"
          @click="selectCategory(category)"
        >
          {{ category.CategoryName }}
        </div>
      </div>

      <!-- 内容区域 -->
      <div class="content-area">
        <div v-if="loading" class="loading">
          <div class="loading-spinner"></div>
          <p>加载中...</p>
        </div>
        
        <div v-else-if="consultationsLoading" class="loading">
          <div class="loading-spinner"></div>
          <p>加载咨询信息中...</p>
        </div>
        
        <div v-else-if="consultations.length === 0" class="empty-state">
          <div class="empty-icon">📋</div>
          <p>暂无咨询信息</p>
        </div>
        
        <div v-else class="consultation-list">
          <div 
            v-for="consultation in consultations" 
            :key="consultation.ConsultId"
            class="consultation-item"
            @click="goToDetail(consultation)"
          >
            <div class="consultation-content">
              <div class="consultation-title">{{ consultation.Title }}</div>
              <div class="consultation-category">#{{ consultation.CategoryName }}</div>
              <div class="consultation-text">{{ consultation.Content }}</div>
            </div>
            <div class="consultation-image">
              <div class="image-placeholder"></div>
            </div>
            <div class="consultation-stats">
              <div class="stat-item">
                <span class="stat-icon">👍</span>
                <span class="stat-value">{{ consultation.LikeCount }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-icon">👁</span>
                <span class="stat-value">{{ consultation.ViewCount }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.consultation-container {
  min-height: 100vh;
  background: #f8f9fa;
  position: relative;
}

/* 状态栏 */
.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background: white;
  font-size: 14px;
  color: #333;
}

.status-icons {
  display: flex;
  gap: 5px;
}

/* 头部 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #eee;
}

.back-button {
  cursor: pointer;
  padding: 5px;
}

.back-icon {
  font-size: 20px;
  color: #333;
}

.page-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 15px;
}

.more-icon, .view-icon {
  font-size: 18px;
  cursor: pointer;
  color: #666;
}

/* 开发模式提示 */
.development-notice {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 120px);
  padding: 20px;
}

.notice-content {
  background: white;
  border-radius: 16px;
  padding: 40px 30px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  max-width: 400px;
  width: 100%;
}

.notice-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.notice-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin: 0 0 15px 0;
}

.notice-text {
  font-size: 16px;
  color: #666;
  line-height: 1.5;
  margin: 0 0 30px 0;
}

.notice-features {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 30px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: #666;
  padding: 10px;
  background: #f8f9fa;
  border-radius: 8px;
}

.feature-icon {
  font-size: 20px;
}

.back-home-btn {
  background: #2196F3;
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 25px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-home-btn:hover {
  background: #1976D2;
  transform: translateY(-2px);
}

/* 分类标签 */
.category-tabs {
  display: flex;
  overflow-x: auto;
  background: white;
  padding: 0 20px;
  border-bottom: 1px solid #eee;
}

.category-tab {
  flex-shrink: 0;
  padding: 15px 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
  font-size: 14px;
  color: #666;
}

.category-tab.active {
  color: #2196F3;
  border-bottom-color: #2196F3;
  font-weight: bold;
}

.category-tab:hover {
  color: #2196F3;
}

/* 内容区域 */
.content-area {
  padding: 20px;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #666;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #2196F3;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #666;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 15px;
}

/* 咨询信息列表样式 */
.consultation-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.consultation-item {
  background: white;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  gap: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  position: relative;
}

.consultation-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.consultation-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.consultation-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  line-height: 1.4;
}

.consultation-category {
  font-size: 12px;
  color: #2196F3;
  background: rgba(33, 150, 243, 0.1);
  padding: 2px 8px;
  border-radius: 12px;
  display: inline-block;
  width: fit-content;
}

.consultation-text {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.consultation-image {
  width: 80px;
  height: 80px;
  flex-shrink: 0;
}

.image-placeholder {
  width: 100%;
  height: 100%;
  background: #e3f2fd;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #2196F3;
  font-size: 24px;
}

.consultation-stats {
  position: absolute;
  bottom: 15px;
  right: 15px;
  display: flex;
  gap: 15px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #666;
}

.stat-icon {
  font-size: 14px;
}

.stat-value {
  font-weight: 500;
}

/* 自定义Toast样式 */
:deep(.van-toast) {
  background: rgba(0, 0, 0, 0.8);
  color: white;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 500;
}

:deep(.van-toast--middle) {
  top: 50%;
  transform: translateY(-50%);
}
</style> 